//
//  ViewController.m
//  DemoUITextField
//
//  Created by tangkexiong on 16/1/6.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate> {
    UITextField *txtFld;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [btn addTarget:self action:@selector(changePwdMod) forControlEvents:UIControlEventTouchUpInside];
    
    txtFld = [[UITextField alloc] initWithFrame:CGRectMake(8, 20, 300, 30)];
    txtFld.rightView = btn;
    txtFld.rightViewMode = UITextFieldViewModeAlways;
    txtFld.borderStyle = UITextBorderStyleBezel;
//    txtFld.returnKeyType = UIReturnKeyDone;
//    txtFld.autocorrectionType = UITextAutocorrectionTypeYes;
//    txtFld.spellCheckingType = UITextSpellCheckingTypeYes;
    txtFld.placeholder = @"提示文字";
    txtFld.delegate = self;
    txtFld.markedTextStyle = @{UITextInputTextBackgroundColorKey:[UIColor redColor]};
    [self.view addSubview:txtFld];
    
    txtFld.text = @"www";
    
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changePwdMod {
    NSString *str = txtFld.text;
    
    txtFld.text = @"";
    txtFld.secureTextEntry = !txtFld.secureTextEntry;
    txtFld.text = str;
}

- (void)textDidChange:(NSNotification *)notification {
    NSLog(@"");
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    [textField endEditing:YES];
    [txtFld setBaseWritingDirection:UITextWritingDirectionRightToLeft forRange:[textField selectedTextRange]];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"当前位置:%@", @(range.location));
    NSLog(@"待选字符串：%@", textField.selectedTextRange);
    NSLog(@"当前字符串：%@", [textField.text stringByReplacingCharactersInRange:range withString:string]);
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

@end
